package com.webserver.core;

import com.webserver.http.HttpServletRequest;

import java.io.*;
import java.net.Socket;
/**
 *  线程任务
 *  负责与特定的客户端进行HTTP交互.
 */
public class ClientHandler implements Runnable{
    private Socket socket;

    public ClientHandler(Socket socket){
        this.socket = socket;
    }

    public void run() {

        try {
            //1.解析请求
            HttpServletRequest request = new HttpServletRequest(socket);

            //2.处理请求
            String path = request.getUri();

            //3.发送响应
            OutputStream out = socket.getOutputStream();
            //将./webapps/myweb/index.html响应给浏览器
            File file = new File("./webapps"+path);

            //状态行相关信息
            int statusCode;
            String statusReason;
            /*
                http:响应格式
                HTTP/1.1 200 OK(CRLF) 状态行
                Content-Type: text/html(CRLF) 响应头
                Content-Length: 2546(CRLF)(CRLF)
                1011101010101010101...... 响应正文
             */

            if (file.exists() && file.isFile()){ //如果定位是文件

                //3.1发送状态行
                statusCode = 200;
                statusReason = "OK";
                String line = "HTTP/1.1"+" "+statusCode+" "+statusReason;
                byte[] data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13); //写入回车符
                out.write(10); //写入换行符

                //3.2发送响应头
                line = "Content-Type: text/html";
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out .write(13);
                out .write(10);

                line = "Content-Length: "+file.length();
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);

                //响应头写完，需要再写一个回车换行符表示写完
                out.write(13);
                out.write(10);

                //3.3发送正文部分
                FileInputStream fis = new FileInputStream(file);
                byte[] buf = new byte[1024*10];
                int len;
                while ((len = fis.read(buf)) != -1){
                    out.write(buf,0,len);
                }
                System.out.println("响应发送完毕!");
            }else { //要么不存在要么是目录
                //发送404页面的状态行/协议版本，状态代码，状态描述
                statusCode = 404;
                statusReason = "NotFound";
                String line = "HTTP/1.1"+" "+statusCode+" "+statusReason;
                byte[] data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);

                //发送响应头
                line = "Content-Type: text/html";
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);

                file = new File("./webapps/root/404.html");
                line = "Content-Length: "+file.length();
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);
                out.write(10);

                //写完响应头后还要读一个回车换行符
                out.write(13);
                out.write(10);

                //发送消息正文
                FileInputStream fis = new FileInputStream("./webapps/root/404.html");
                byte[] fus = new byte[1024*10];
                int len;
                while ((len = fis.read(fus)) != -1){
                    out.write(fus,0,len);
                }
                System.out.println("发送404页面完毕");

            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //响应后断开连接
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
